package stu.cn.ua.rgr;

import process.Actor;
import process.DispatcherFinishException;
import process.IWaitCondition;
import queues.QueueForTransactions;
import queues.TransProcessQueue;
import widgets.ChooseRandom;
import transProcess.ITransProcesable;


public class Gruzovik extends Actor {
	private Main gui;
	private Model model;
	private QueueForTransactions queueOfGruzovikDowload;
	private QueueForTransactions queueOfGruzovikUpload;
	private QueueForTransactions queueOfSivalka;
//	private int sizeOfGruzovik;
	private boolean load;
	private double finishTime;
	private ChooseRandom rnd;
	private ChooseRandom rndUp;
	private TransProcessQueue queueGruzovik;
	
	
	public TransProcessQueue getQueueGruzovik(){
		return queueGruzovik;
	}
	public void setQueueGruzovik(TransProcessQueue queueGruzovik){
		this.queueGruzovik=queueGruzovik;
	}

	
	public Gruzovik(String name, Main gui, Model model) {
		this.setNameForProtocol(name);
		this.gui = gui;
		this.model = model;
		this.queueOfGruzovikDowload = model.getQueueOfGruzovikDownload();
		this.queueOfGruzovikUpload = model.getQueueOfGruzovikUpload();
		this.queueOfSivalka = model.getQueueOfSivalka();
		finishTime=Double.parseDouble(gui.getJTextTime().getText());
		this.rnd = gui.getChooseRandomGrMove();
		this.rndUp = gui.getChooseRandomGrUpload();

	}
	
	@Override
	protected void rule() {
		getDispatcher().printToProtocol("Вантажівка почала роботу ");
		IWaitCondition full = new IWaitCondition() {
			
			@Override
			public boolean testCondition() {
				return load;
			}
			public String toString() {
				
				return " наповнена";	
				}
		};
		IWaitCondition queueSizeGt0 = new IWaitCondition() {
			
			@Override
			public boolean testCondition() {
				
				return (queueOfSivalka.size()>0) && (Gruzovik.this == queueOfGruzovikUpload.peekFirst());
			}
			public String toString() {
				
			return " чекає сівалку на завантаження";	
			}
			
		};
		while (getDispatcher().getCurrentTime()<finishTime) {
			//edem na sklad
			getDispatcher().printToProtocol(this+" їде на загрузку");
			load=false;
			//задержка на езду 
			holdForTime(rnd.next());
			getDispatcher().printToProtocol(this +" стала в чергу на загрузку");
			
			queueOfGruzovikDowload.addLast(this);
			try {
				waitForCondition(full);
			} catch (DispatcherFinishException e) {
				return;
			}
			//Edem na pole
			getDispatcher().printToProtocol(this+" їде на поле");
			//задержка на езду
			holdForTime(rnd.next());
			queueOfGruzovikUpload.addLast(this);
			//model.getHisto().add(queueOfGruzovikUpload.size());
			try {
				waitForCondition(queueSizeGt0);
			} catch (DispatcherFinishException e) {
				
				return;
			}
			Sivalka s = (Sivalka) queueOfSivalka.removeFirst();
			//model.getHisto1().add(queueOfSivalka.size());
			//Задержка на загрузку сеялки
			holdForTime(rndUp.next());
			s.setLoad();
			getDispatcher().printToProtocol(s+" наповнена");
			queueOfGruzovikUpload.removeFirst();
			//model.getHisto().add(queueOfGruzovikUpload.size());
		}
			
	}
	
	public void setLoad() {
		load = true;	
	}
}
